<?php
// 帖子搜索
include '../../init.php';

$word = $_GET['word'];
$type = $_GET['type'];
if (empty($word)){
    show_msg(1, '搜索内容不能为空');
    exit();
}
$page = @$_GET['page']-1;
if ($page < 1){
    $page = 0;
}
$count = @$_GET['count'];
if ($count < 1){
    $count = 20;
}

// 默认帖子搜索
$where  = '';
$fields = '';
$tables = '';
$limit  = "LIMIT ".($page*$count).",{$count};";
$data   = array();

if ($type == 'threads'){
    $fields = 'pid,parentId,modid,uid,title,content,create_time ';
    $tables = '`post_tb` ';
    $where  = "WHERE title LIKE '%{$word}%' OR content LIKE '%{$word}%' ORDER BY create_time DESC ";
} else {
    $fields = 'uid,alias,email ';
    $tables = "`userinfo_tb` ";
    $where  = "WHERE alias LIKE '%{$word}%' RDER BY last_time DESC ";
}
$sql = 'SELECT '.$fields.'FROM '.$tables.$where.$limit;
my_mysql_query($sql, $data);


// 获取总的数据
$sum = array();
$sql = "SELECT count(uid) as sum FROM {$tables} ".$where;
my_mysql_query($sql, $sum);
$sum = $sum[0]['sum'];
?>


<!DOCTYPE html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>搜索结果</title>

    <link rel="stylesheet" type="text/css" href="../main/css/style_3_common.css">
    <link rel="stylesheet" type="text/css" href="../main/css/gfan_bbs.css">

    <link rel="stylesheet" type="text/css" href="../main/css/main_home.css">
    <link rel="stylesheet" type="text/css" href="../main/css/main_search.css">
    <link rel="stylesheet" type="text/css" href="../main/css/main_phone.css">
    <link rel="stylesheet" type="text/css" href="../main/css/main_gfan.css">
    <style type="text/css">
    *{
        line-height: 36px;
        font-size: 14px;
    }
    </style>
</head>

<body>
    <div>
        <div id="ct" class="ptm wp w cl">
            <div class="mn" id="main_message">
                <div class="bm" style="background-color: white;">

                    <!-- 标题栏 -->
                    <div class="bm_h bbs">
                        <h3 class="xs2"><a href="../../index.php">首页</a></h3>
                    </div>

                    <!-- 搜索框 -->
                    <div class="search">
                        <form action="./search.php" method="GET" id="searchForum">
                            <input type="text" class="si sci" value="<?php echo $word; ?>" id="q" name="word">
                            <select class="si sri" id="t" name="type">
                                <option value="threads" <?php echo $type=='threads'?'selected':''; ?>>帖子</option>
                                <option value="members" <?php echo $type=='members'?'selected':''; ?>>用户</option>
                            </select>
                            <button type="submit" class="si sbt" id="sBt">搜索</button>
                        </form>
                    </div>


                    <form action="./search.php" method="GET">
                        <input type="hidden" name="word" value="<?php echo $word; ?>">
                        <input type="hidden" name="type" value="<?php echo $type; ?>">共有&nbsp;
                        <span style="color:red"><?php echo ceil($sum/$count); ?></span>&nbsp;页,每页有
                        <input type="text" size="2" name="count" value="<?php echo $count; ?>">记录
                        当前第&nbsp;<span style="color:red"><?php echo ($page + 1); ?></span>&nbsp;页,跳到第
                        <input type="text" size="3" name="page" value="<?php echo $page + 2; ?>">页
                        <input type="submit" value="GO">
                    </form>
                    <!-- 结果页面 -->
                    <div>
                        <div id="main_messaqge_Lyel8">

                        <?php


                        if (count($data) <1) {
                            echo '<br />没有相关内容<br/><br/>';
                        }else{
                            // 如果是帖子直接转到哦帖子详情页面
                            // 如果是用户直接跳到用户详情页面
                            foreach($data as $value){
                                if ($type == 'threads'){
                                    echo '&nbsp;>>&nbsp;&nbsp;';
                                    $content = $value['title']?'[发帖]:'.$value['title']:'[回帖]:'.strip_tags($value['content']);
                                    $coutent = substr($content,0,20);
                                    echo '<a href="../post_list/index.php?zid='.$value['modid'].'&pid=';

                                    // 发帖与回帖,传的数据不一致注意
                                    if (empty($value['title'])){
                                        echo $value['parentId'].'">'.$content.'</a><br/>';
                                    } else {
                                        echo $value['pid'].'">'.$content.'</a><br/>';
                                    }
                                } else {
                                    echo '&nbsp;>>&nbsp;&nbsp;<a href="../user/index.php?uid='.$value['uid'].'">'.$value['alias'].'</a><br/>';
                                }
                            }
                        }

                        ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>






